package cn.edu.klmy.controller.importdata;

import cn.edu.klmy.models.teacher.Teacher;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;

import web.enumeditor.Nation;
import web.enumeditor.Sex;
import web.util.ExcelReader;
import web.util.IExcelRowMapper;
import web.util.SpaceChecker;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class ExcelMapperTeacher implements IExcelRowMapper {
	private static org.apache.log4j.Logger LOG = Logger.getLogger(ExcelMapperStudent.class);
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    private List<ImportError> importErrors = new ArrayList<ImportError>();

    @Override
    public Teacher mapFromXls(Row row) throws Exception {
        if (null == row)
            return null;
        HSSFRow hssfRow = (HSSFRow) row;
        Teacher teacher = new Teacher();
        if (null != hssfRow.getCell(0) && ExcelReader.getValueFromHssf(hssfRow.getCell(0)) != null) {
        	String name = String.valueOf(ExcelReader.getValueFromHssf(hssfRow.getCell(0)));
        	if (!name.trim().equals("")) {
        		teacher.setName(SpaceChecker.delSpace(name));
        	} else {
        		importErrors.add(new ImportError(row, 0, "姓名为空!"));
        		LOG.debug("Excel[教师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 姓名为空!");
        		return null;
        	}
        } else {
    		importErrors.add(new ImportError(row, 0, "姓名单元格数据异常或为空!"));
    		LOG.debug("Excel[教师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 姓名单元格数据异常或为空!");
    		return null;
        }
        if (null != hssfRow.getCell(1) && ExcelReader.getValueFromHssf(hssfRow.getCell(1)) != null) {
        	String identityCardNumber = String.valueOf(ExcelReader.getValueFromHssf(hssfRow.getCell(1)));
        	if (!identityCardNumber.trim().equals("")) {
        		teacher.setIdentityCardNumber(SpaceChecker.delSpace(identityCardNumber));
//        		teacher.setIdentityCardNumber(SpaceChecker.subSzOrZm(SpaceChecker.delSpace(identityCardNumber)));
        	} else {
        		importErrors.add(new ImportError(row, 0, "身份证号为空!"));
        		LOG.debug("Excel[教师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,身份证号为空!");
        		return null;
        	}
        } else {
    		importErrors.add(new ImportError(row, 0, "身份证号单元格数据异常或为空!"));
    		LOG.debug("Excel[教师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,身份证号单元格数据异常或为空!");
    		return null;
        }
        if (null != hssfRow.getCell(2) && ExcelReader.getValueFromHssf(hssfRow.getCell(2)) != null) {
        	Object nationObj = ExcelReader.getValueFromHssf(hssfRow.getCell(2));
        	String nation = String.valueOf(nationObj);
        	if (!nation.trim().equals("")) {
        		Nation n = Nation.getNationByMarch(SpaceChecker.delSpace(nation));
        		if (n == null) {
            		importErrors.add(new ImportError(row, 1, "民族字段单元格数据异常或为空:使用默认值[其他]导入!"));
            		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,民族字段单元格数据异常或为空:使用默认值[其他]导入!");
            		teacher.setNation(Nation.Other.getMark());
            	} else {
            		teacher.setNation(n.getMark());
            	}
        	} else {
        		importErrors.add(new ImportError(row, 1, "民族字段单元格数据异常或为空:使用默认值[其他]导入!"));
        		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,民族字段单元格数据异常或为空:使用默认值[其他]导入!");
        		teacher.setNation(Nation.Other.getMark());
        	}
        } else {
    		importErrors.add(new ImportError(row, 1, "民族字段单元格数据异常或为空:使用默认值[其他]导入!"));
    		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,民族字段单元格数据异常或为空:使用默认值[其他]导入!");
    		teacher.setNation(Nation.Other.getMark());
    	}
        Date d = null;
        if (null != hssfRow.getCell(3) && ExcelReader.getValueFromHssf(hssfRow.getCell(3)) != null) {
        	Object birthdayObj = ExcelReader.getValueFromHssf(hssfRow.getCell(3));
        	try {
        		d = (Date) birthdayObj;
        	} catch(ClassCastException e) {
        		e.printStackTrace();
        	}
        	if (d == null || df.format(d) == null) {
        		teacher.setBirthday(splitBirthdayFromIdNo(teacher.getIdentityCardNumber()));
        	} else {
        		teacher.setBirthday(df.format(d));
        	}
        } else {
        	teacher.setBirthday(splitBirthdayFromIdNo(teacher.getIdentityCardNumber()));
        }
        if (null != hssfRow.getCell(4) && ExcelReader.getValueFromHssf(hssfRow.getCell(4)) != null) {
    		Object sexObj = ExcelReader.getValueFromHssf(hssfRow.getCell(5));
    		String sex = String.valueOf(sexObj);
        	if (!sex.equals("")) {
        		Sex s = getSex(SpaceChecker.delSpace(sex));
        		if (s == null) {
            		importErrors.add(new ImportError(row, 0, "性别单元格数据异常或为空!"));
            		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 性别单元格数据异常或为空!");
            		return null;
        		} else {
        			teacher.setSex(s.ordinal());
        		}
        	} else {
        		importErrors.add(new ImportError(row, 0, "性别单元格数据异常或为空!"));
        		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 性别单元格数据异常或为空!");
        		return null;
        	}
        } else {
    		importErrors.add(new ImportError(row, 0, "性别单元格数据异常或为空!"));
    		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 性别单元格数据异常或为空!");
    		return null;
        }
        if (null != hssfRow.getCell(6) && ExcelReader.getValueFromHssf(hssfRow.getCell(6)) != null) {
        	teacher.setGrade((String) ExcelReader.getValueFromHssf(hssfRow.getCell(6)));
        } else {
    		importErrors.add(new ImportError(row, 0, "班级单元格数据异常或者为空!"));
    		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 班级单元格数据异常或者为空!");
    		return null;
        }
        if (null != hssfRow.getCell(5) && ExcelReader.getValueFromHssf(hssfRow.getCell(5)) != null) {
        	teacher.setSubject((String) ExcelReader.getValueFromHssf(hssfRow.getCell(5)));
        } else {
    		importErrors.add(new ImportError(row, 0, "科目单元格数据异常或者为空!"));
    		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 科目单元格数据异常或者为空!");
    		return null;
        }
        return teacher;
    }
    
    private String splitBirthdayFromIdNo(String identityCardNumber) {
    	return identityCardNumber.substring(6, 10) + "-" + identityCardNumber.substring(10, 12) + "-" + identityCardNumber.substring(12, 14);
    }

    @Override
    public Teacher mapFromXlsx(Row row) throws Exception {
        XSSFRow xssfRow = (XSSFRow) row;
        Teacher teacher = new Teacher();
        if (null != xssfRow.getCell(0) && ExcelReader.getValueFromXssf(xssfRow.getCell(0)) != null) {
        	String name = String.valueOf(ExcelReader.getValueFromXssf(xssfRow.getCell(0)));
        	if (!name.trim().equals("")) {
        		teacher.setName(SpaceChecker.delSpace(name));
        	} else {
        		importErrors.add(new ImportError(row, 0, "姓名为空!"));
        		LOG.debug("Excel[教师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 姓名为空!");
        		return null;
        	}
        } else {
    		importErrors.add(new ImportError(row, 0, "姓名单元格数据异常或为空!"));
    		LOG.debug("Excel[教师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 姓名单元格数据异常或为空!");
    		return null;
        }
        if (null != xssfRow.getCell(1) && ExcelReader.getValueFromXssf(xssfRow.getCell(1)) != null) {
        	String identityCardNumber = String.valueOf(ExcelReader.getValueFromXssf(xssfRow.getCell(1)));
        	if (!identityCardNumber.trim().equals("")) {
        		teacher.setIdentityCardNumber(SpaceChecker.delSpace(identityCardNumber));
//        		teacher.setIdentityCardNumber(SpaceChecker.subSzOrZm(SpaceChecker.delSpace(identityCardNumber)));
        	} else {
        		importErrors.add(new ImportError(row, 0, "身份证号为空!"));
        		LOG.debug("Excel[教师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,身份证号为空!");
        		return null;
        	}
        } else {
    		importErrors.add(new ImportError(row, 0, "身份证号单元格数据异常或为空!"));
    		LOG.debug("Excel[教师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,身份证号单元格数据异常或为空!");
    		return null;
        }
        if (null != xssfRow.getCell(2) && ExcelReader.getValueFromXssf(xssfRow.getCell(2)) != null) {
        	Object nationObj = ExcelReader.getValueFromXssf(xssfRow.getCell(2));
        	String nation = String.valueOf(nationObj);
        	if (!nation.trim().equals("")) {
        		Nation n = Nation.getNationByMarch(SpaceChecker.delSpace(nation));
        		if (n == null) {
            		importErrors.add(new ImportError(row, 1, "民族字段单元格数据异常或为空:使用默认值[其他]导入!"));
            		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,民族字段单元格数据异常或为空:使用默认值[其他]导入!");
            		teacher.setNation(Nation.Other.getMark());
            	} else {
            		teacher.setNation(n.getMark());
            	}
        	} else {
        		importErrors.add(new ImportError(row, 1, "民族字段单元格数据异常或为空:使用默认值[其他]导入!"));
        		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,民族字段单元格数据异常或为空:使用默认值[其他]导入!");
        		teacher.setNation(Nation.Other.getMark());
        	}
        } else {
    		importErrors.add(new ImportError(row, 1, "民族字段单元格数据异常或为空:使用默认值[其他]导入!"));
    		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常,民族字段单元格数据异常或为空:使用默认值[其他]导入!");
    		teacher.setNation(Nation.Other.getMark());
    	}
        Date d = null;
        if (null != xssfRow.getCell(3) && ExcelReader.getValueFromXssf(xssfRow.getCell(3)) != null) {
        	Object birthdayObj = ExcelReader.getValueFromXssf(xssfRow.getCell(3));
        	try {
        		d = (Date) birthdayObj;
        	} catch(ClassCastException e) {
        		e.printStackTrace();
        	}
        	if (d == null || df.format(d) == null) {
        		teacher.setBirthday(splitBirthdayFromIdNo(teacher.getIdentityCardNumber()));
        	} else {
        		teacher.setBirthday(df.format(d));
        	}
        } else {
        	teacher.setBirthday(splitBirthdayFromIdNo(teacher.getIdentityCardNumber()));
        }
        if (null != xssfRow.getCell(4) && ExcelReader.getValueFromXssf(xssfRow.getCell(4)) != null) {
    		Object sexObj = ExcelReader.getValueFromXssf(xssfRow.getCell(4));
    		String sex = String.valueOf(sexObj);
        	if (!sex.equals("")) {
        		Sex s = getSex(SpaceChecker.delSpace(sex));
        		if (s == null) {
            		importErrors.add(new ImportError(row, 0, "性别单元格数据异常或为空!"));
            		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 性别单元格数据异常或为空!");
            		return null;
        		} else {
        			teacher.setSex(s.ordinal());
        		}
        	} else {
        		importErrors.add(new ImportError(row, 0, "性别单元格数据异常或为空!"));
        		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 性别单元格数据异常或为空!");
        		return null;
        	}
        } else {
    		importErrors.add(new ImportError(row, 0, "性别单元格数据异常或为空!"));
    		LOG.debug("Excel[老师]导入功能>>>>>异常!位置: [" + row.getRowNum() + "] 异常, 性别单元格数据异常或为空!");
    		return null;
        }
        if (null != xssfRow.getCell(6) && ExcelReader.getValueFromXssf(xssfRow.getCell(6)) != null) {
        	teacher.setSubject((String) ExcelReader.getValueFromXssf(xssfRow.getCell(6)));
//        } else {
//    		importErrors.add(new ImportError(row, 1, "科目单元格数据异常或者为空!"));
//    		LOG.debug("Excel[老师]导入功能>>>>>警告!位置: [" + row.getRowNum() + "] 异常, 科目单元格数据异常或者为空!");
        }
        if (null != xssfRow.getCell(5) && ExcelReader.getValueFromXssf(xssfRow.getCell(5)) != null) {
        	teacher.setGrade((String) ExcelReader.getValueFromXssf(xssfRow.getCell(5)));
//        } else {
//    		importErrors.add(new ImportError(row, 1, "班级单元格数据异常或者为空!"));
//    		LOG.debug("Excel[老师]导入功能>>>>>警告!位置: [" + row.getRowNum() + "] 异常, 班级单元格数据异常或者为空!");
        }
        return teacher;
    }

    public Sex getSex(String name) {
    	boolean isInt = Pattern.matches("^[0-9]*$", name);
    	Sex s = null;
    	if (isInt){
    		s = Sex.getSexByOrdinal(Integer.parseInt(name));
    	} else {
    		s = Sex.getSexByChineseName(name);
    	}
    	return s;
    }
    
    public List<ImportError> getImportErrors() {
		return this.importErrors;
	}
}
